import * as THREE from "three";
import Experience from "./Experience";
export default class Renderer {
  constructor() {
    this.experience = Experience.instance; //由于它是单例，所以即便在这里导入进来，也是原来的那个唯一对象。
    this.canvas = this.experience.canvas;
    this.sizes = this.experience.sizes;
    this.camera = this.experience.camera;
    this.scene = this.experience.scene;

    this.setInstance();
  }

  setInstance() {
    this.instance = new THREE.WebGLRenderer({
      canvas: this.canvas,
      antialias: true, //开启抗锯齿
    });
    this.instance.physicallyCorrectLights = true; //开启物理灯光
    this.instance.shadowMap.enabled = true; //开启阴影
    this.instance.shadowMap.type = THREE.PCFSoftShadowMap; //设置阴影类型
    this.instance.setSize(this.sizes.width, this.sizes.height); //重新设置渲染器大小
    this.instance.setPixelRatio(this.sizes.pixelRatio);
    this.instance.outputColorSpace = THREE.SRGBColorSpace;
    this.instance.toneMapping = THREE.CineonToneMapping; //设置颜色映射
    this.instance.toneMappingExposure = 1.75; //曝光度
    this.instance.setClearColor(new THREE.Color(0x111111));//设置背景颜色
  }

  resize() {
    this.instance.setSize(this.sizes.width, this.sizes.height); //重新设置渲染器大小
    this.instance.setPixelRatio(this.sizes.pixelRatio);
  }

  update(){
    this.instance.render(this.scene, this.camera.instance);
  }
}
